/*
 * @Author: 黄达全
 * @Date: 2024-08-30 16:00:00
 * @LastEditTime: 2024-08-30 18:50:00
 */
import axios from "../index"
import type { ApiResponse } from "@/common/type"

// 定义商品类型
export interface GoodsItem {
  id: number
  title: string
  categoryId: number
  cover: string
  rating: number
  saleCount: number
  reviewCount: number
  minPrice: number
  minOprice: number
  desc: string
  unit: string
  stock: number
  minStock: number
  ischeck: number
  status: number
  stockDisplay: number
  expressId: number
  skuType: number
  skuValue: string
  content: string
  discount: number
  createTime: string
  updateTime: string
  deleteTime: string
  order: number
  createManagerId: number
  lastUpdateManagerId: number
}

// 定义分页返回数据类型
export interface GoodsPageVO {
  total: number
  records: GoodsItem[]
}

// 定义新增商品表单类型
export interface GoodsAddForm {
  title: string
  categoryId: number
  cover: string
  rating?: number
  saleCount?: number
  reviewCount?: number
  minPrice: number
  minOprice?: number
  desc: string
  unit?: string
  stock: number
  minStock?: number
  ischeck?: number
  status: number
  stockDisplay?: number
  expressId?: number
  skuType?: number
  skuValue?: string
  content?: string
  discount?: number
  order?: number
}


export interface GoodsFilterForm {
    currentPage:number,
    pageSize:number,
    categoryId?:number  |null,
    title?:string |null,
    status?:number |null,
    ischeck?:number |null,
    stockWarn?:boolean |null
    isDelete?:number |null,
    createManagerId?:number |null,
}

// 定义商品API接口
export interface GoodsApi {
  getList: (page: number, pageSize: number) => Promise<ApiResponse<GoodsPageVO>>
  add: (form: GoodsAddForm) => Promise<ApiResponse<null>>
  update: (form: GoodsItem) => Promise<ApiResponse<null>>
  delete: (ids: number[]) => Promise<ApiResponse<null>>
  deleteByLuoJi: (id:number,status:number) => Promise<ApiResponse<null>>
  
  getById: (id: number) => Promise<ApiResponse<GoodsItem>>
  filterGoods: (form: GoodsFilterForm) => Promise<ApiResponse<GoodsPageVO>>
}

export default {
  // 获取商品分页列表
  async getList(page, pageSize) {
    return (await axios.get(`/admin/goods/page?page=${page}&pageSize=${pageSize}`)).data as ApiResponse<GoodsPageVO>
  },
  
  // 新增商品
  async add(form: GoodsAddForm) {
    return (await axios.post(`/admin/goods`, form)).data as ApiResponse<null>
  },
  
  // 更新商品
  async update(form: GoodsItem) {
    return (await axios.put(`/admin/goods`, form)).data as ApiResponse<null>
  },
  
  // 删除商品
  async delete(ids: number[]) {
    const queryString = ids.map(id => `ids=${id}`).join('&');  
    return (await axios.delete(`/admin/goods?${queryString}`)).data as ApiResponse<null>
  },
  // 逻辑删除商品
  async deleteByLuoJi(id:number,status:number) {
    return (await axios.post(`/admin/goods/${id}?status=${status}`)).data as ApiResponse<null>
  },
  
  // 获取商品详情
  async getById(id: number) {
    const res = (await axios.get(`/admin/goods/${id}`)).data as ApiResponse<any>;
    if (res.code === 200 && res.data) {
      // 将API返回的下划线格式转换为前端使用的小驼峰格式
      const data = res.data;
      const goodsItem: GoodsItem = {
        id: data.id,
        title: data.title,
        categoryId: data.category_id,
        cover: data.cover,
        rating: data.rating,
        saleCount: data.sale_count,
        reviewCount: data.review_count,
        minPrice: data.min_price,
        minOprice: data.min_oprice,
        desc: data.desc,
        unit: data.unit,
        stock: data.stock,
        minStock: data.min_stock,
        ischeck: data.ischeck,
        status: data.status,
        stockDisplay: data.stock_display,
        expressId: data.express_id,
        skuType: data.sku_type,
        skuValue: data.sku_value,
        content: data.content,
        discount: data.discount,
        createTime: data.create_time,
        updateTime: data.update_time,
        deleteTime: data.delete_time,
        order: data.order,
        createManagerId: data.create_manager_id,
        lastUpdateManagerId: data.last_update_manager_id
      };
      return {
        code: res.code,
        msg: res.msg,
        data: goodsItem
      } as ApiResponse<GoodsItem>;
    }
    return res as ApiResponse<GoodsItem>;
  },

  // 获取过滤后的商品
  async filterGoods(form:GoodsFilterForm){
    return (await axios.post('/admin/goods/filter', form)).data as ApiResponse<GoodsPageVO>
  }
} as GoodsApi 